
#include "NetMsgHandler.h"
#include "NetLoginHandler.h"
#include "NetChatHandler.h"
#include "NetMapHandler.h"
#include "NetActionHandler.h"
#include "NetBattleHandler.h"
#include "NetPetHandler.h"
#include "NetItemHandler.h"
#include "NetNpcHandler.h"

#include "CreatureManager.h"
#include "DBLoginHandler.h"
#include "MapManager.h"
#include "DBManager.h"

#include "netServer.h"
extern netServer* gNetServer;
extern dbIDatabase* gDBServer;

static map< fint32 , NetMsgHandler > gMsgHeandler;

fbool			CheckMsg( netMsgHead* msg , fint32 size )
{
	if ( msg->size == size )
	{
		return F_TRUE;
	}
	else
	{
		return F_FALSE;
	}
}


fvoid			InitNetMsg()
{
	static NetMsgHandler handler[] =
	{
		{ _MSG_SEND_CREATE_ACCOUNT , NetCreateAccountHandler , "" },
		{ _MSG_SEND_CREATE_ROLE , NetCreateRoleHandler , ""  },
		{ _MSG_SEND_LOGIN_GAMESERVICE , NetLoginHandler , ""  },

		{ _MSG_SEND_ROLE_INFO , NetGetRoleInfoHandler , ""  },

		{ _MSG_SEND_CHAT_MSG , NetSendChatMsgHandler , ""  },

		{ _MSG_SEND_MOVE , NetMoveHandler , ""  },
		{ _MSG_SEND_JOIN_TEAM , NetJoinTeamHandler , ""  },
		{ _MSG_SEND_LEAVE_TEAM , NetLevelTeamHandler , ""  },

		{ _MSG_SEND_ACTION , NetActionMsgHandler , ""  },
		{ _MSG_SEND_DIRECTION , NetDirectionHandler , ""  },

		{ _MSG_SEND_BATTLE , NetBattleHandler , ""  },
		{ _MSG_SEND_BATTLE_COMMAND , NetBattleCommandHandler , ""  },

		{ _MSG_SEND_LEVELUP_POINT , NetLevelUpPointHandler , ""  },

		{ _MSG_SEND_SET_PET_TYPE , NetSetPetTypeHandler , ""  },
		{ _MSG_SEND_SET_PET_NAME , NetSetPetNameHandler , ""  },
		{ _MSG_SEND_PET_INFO , NetPetInfoHandler , ""  },


		{ _MSG_SEND_SWITCH_ITEM , NetSwitchItemHandler , ""  },
		{ _MSG_SEND_SWITCH_EQUIP , NetSwitchEquipHandler , ""  },
		{ _MSG_SEND_USE_ITEM , NetUseItemHandler , ""  },
		{ _MSG_SEND_DROP_EQUIP , NetDropEquipHandler , ""  },

		{ _MSG_SEND_TALK_NPC , NetTalkNpcHandler , "" },

		{ 0 , F_NULL , "" }

	};

	
    fint32 i = 0;
	while ( handler[i].type )
	{
		gMsgHeandler[ handler[ i ].type ] = handler[ i ];
        
		i++;
	}
}


fvoid			OnNetMsgEnter( netSocket* socket )
{
	GamePlayer* player = gCreatureManager->GetPlayer( socket );

	if ( player )
	{
        // foxsdk error
        
		ASSERT( 0 );
		socket->Close();
		return;
	}

	player = gCreatureManager->CreatPlayer();

	if ( player )
	{
		fbool bload = gCreatureManager->SetSocket( socket , player );

		if ( !bload )
		{
			// foxsdk error
            
            assert( 0 );
			socket->Close();
			return;
		}
		else
		{
			player->Init();
			//FLOG0( "player load %s" , socket->GetIP() );
		}

	}
	else
	{
		//@ error

		socket->Close();
	}

}


fvoid           OnNetMsg( netSocket* socket , netMsgHead* head )
{
    GamePlayer* player = gCreatureManager->GetPlayer( socket );
    
    map< fint32 , NetMsgHandler >::iterator iter = gMsgHeandler.find( head->type );
    
    if ( iter == gMsgHeandler.end() )
    {
        // bad player,
        socket->Close();
    }
    else
    {
        (iter->second.f)( player , head );
        
		//string s = socket->local_endpoint().address().to_string().c_str();
        
		//log( LOGLEVEL0 , "OnNetMsg %s %s " , iter->second.des.c_str() , s.c_str() );
        //FLOG0( "OnNetMsg %s %s " , iter->second.des.c_str() , s.c_str() );
    }
}



fvoid			OnNetMsgExit( netSocket* socket )
{
	GamePlayer* player = gCreatureManager->GetPlayer( socket );

	if ( !player )
	{
        // foxsdk error
        
		assert( 0 );
		return;
	}

	gMapManager->LeaveTeam( player );
	gMapManager->ExitScene( player );

	gCreatureManager->RemoveSocket( player );
	
	//logManager::Print( "player quit %s" , socket->GetIP() );

// 	if ( player->GetGUID() == INVALID_ID || 
// 		player->GetGUID() >= GUEST_GUID )
// 	{
// 		gCreatureManager->ReleasePlayer( player );
// 		gCreatureManager->RemovePlayer( player );
// 		player->Release();
// 	}
// 	else
	{
// 		sprintf_s( SQL_BUFFER , MAX_SQL_BUFFER , SQL_UPDATE_TECH , player->GetGUID() , player->GetTechSaveString() );
// 		SQL_BUFFER[ MAX_SQL_BUFFER - 1 ] = '\0';
// 		gSerManager->GetDB()->ExecuteAsyncSQL( SQL_BUFFER , player , DB_Update_Tech );

		GamePetMap& petMap = player->GetPetMap();
		GamePetMapIter iterEnd = petMap.end();

		for ( GamePetMapIter iter = petMap.begin() ; iter != iterEnd ; ++iter )
		{
			SPRINTF( SQL_BUFFER , SQL_UPDATE_PET.c_str() , iter->first , iter->second.GetData().Name , DBManager::GetPetDBData( &(iter->second) ) );
			SQL_BUFFER[ MAX_SQL_BUFFER - 1 ] = '\0';
			gDBServer->ExecuteAsyncSQL( SQL_BUFFER , player , DB_Update_Pet );
		}

		SPRINTF( SQL_BUFFER , SQL_UPDATE_QUITGAME.c_str() , player->GetGUID() , DBManager::GetPlayerDBData( player ) , DBManager::GetItemDBData( player ) );
		SQL_BUFFER[ MAX_SQL_BUFFER - 1 ] = '\0';
		gDBServer->ExecuteAsyncSQL( SQL_BUFFER , player , DB_Update_QuitGame );
	}
}


